library PhysicsPsuedoNatives


    globals
        private constant location L = Location(0,0)
	endglobals
	
	// sets unit'z Z coordinate. if unable to set due to ground, then return false
    // otherwise return true
    function SetUnitZ takes unit u, real z returns boolean
        local real Z
        call MoveLocation(L,GetUnitX(u),GetUnitY(u))
        set Z = z - GetLocationZ(L)
        if(Z<0) then
            call SetUnitFlyHeight(u,0,0)
            return false
        endif
        call SetUnitFlyHeight(u,Z ,0.0)
        return true
    endfunction 
    // returns unit's z coordinate
    function GetUnitZ takes unit u returns real
        call MoveLocation(L,GetUnitX(u),GetUnitY(u))
        return GetLocationZ(L) + GetUnitFlyHeight(u)
    endfunction
    // returns location's z coordnates
    function GetZ takes real x, real y returns real
        call MoveLocation(L, x,y)
        return GetLocationZ(L)
    endfunction
	
	struct QuadraticEquation extends array
        
        readonly static real x1 = 0.0
        readonly static real x2 = 0.0
		readonly static boolean HAS_SOLUTION = true
        
        private static method create takes nothing returns thistype
			return 1
        endmethod
        
        static method solve takes real a, real b, real c returns thistype
			local thistype t = 1
            local real t_root = b*b - 4 * a * c
            
            if( t_root <=0) then
                set t.HAS_SOLUTION = false
                return t
            endif
            set t_root = SquareRoot(t_root)
            
            set t.x1 = ( -b + t_root ) / ( 2*a )
            set t.x2 = ( -b - t_root ) / ( 2*a )
            
            if ( t.x1 > t.x2) then
                set t_root = t.x1
                set t.x1 = t.x2
                set t.x2 = t_root
            endif
            
            return t
        endmethod
    endstruct

    // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    //
    // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    
    // returns the positive value of x
    function AbsoluteValue takes real x returns real
        if (x <= 0) then
            return -1*x
        endif
        return x
    endfunction
    
    struct Math
        real pi = 3.1415
    endstruct
    
    // returns 1d distance between points
    function DistanceBetweenPoints1 takes real sx, real dx returns real
        return AbsoluteValue(sx - dx)
    endfunction
    
    // returns 2d distance between points
    function DistanceBetweenPoints2 takes real sx, real sy, real dx, real dy returns real
        return SquareRoot((dx - sx)*(dx - sx) + (dy - sy) * (dy - sy))
    endfunction
    
    // returns 3d distance between points
    function DistanceBetweenPoints3 takes real sx, real sy, real sz, real dx, real dy, real dz returns real
        return SquareRoot((dx - sx)*(dx - sx) + (dy - sy) * (dy - sy) + (dz - sz) * (dz - sz))
    endfunction
    
    // returns radian value of angle between points respecting the game's X axis.
    function AngleBetweenPointsRad takes real x1, real y1, real x2, real y2 returns real
        return Atan2(y2 - y1, x2 - x1)
    endfunction
    
    // returns degree value of angle between points respecting the game's X axis.
    function AngleBetweenPointsDeg takes real x1, real y1, real x2, real y2 returns real
        return bj_RADTODEG * Atan2(y2 - y1, x2 - x1)
    endfunction
	

endlibrary